package com.peanut.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabUserBO;
import com.peanut.domain.po.TabUserPO;
import com.peanut.dto.TabUserDTO;
import com.peanut.service.TabUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("user")
@Api("后台用户管理Api")
public class UserController {

    @Resource
    private TabUserService userService;

    /**
     * 用户添加方法
     * @param userDTO : 用户实体类中的数据
     * @return
     */
    @PostMapping
    @ApiOperation("添加用户")
    @ApiImplicitParam(name = "userDTO",value = "用户实体类",required = true,paramType ="body")
    public Result userAdd(@RequestBody TabUserDTO userDTO){
        try {
            TabUserBO bo = new TabUserBO();
            BeanUtils.copyProperties(userDTO,bo);
            bo.setUserName(userDTO.getPhone().toString());
            bo.setJurisdiction(1);
            int i = userService.userAdd(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询所有用户，并分页
     * @param pageNumber : 当前页面
     * @param pageSize : 每页条数
     * @return
     */
    @GetMapping("findAll")
    @ApiOperation("查询所有用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页面",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1") Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabUserPO> user = userService.findAll(pageNumber,pageSize);
            return Result.OK().setData(user);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改用户数据
     * @param userDTO : 用户实体类中的数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改用户数据")
    @ApiImplicitParam(name = "userDTO",value = "用户实体类",required = true,paramType ="body")
    public Result updateUser(@RequestBody TabUserDTO userDTO){
        try {
            TabUserBO bo = new TabUserBO();
            BeanUtils.copyProperties(userDTO,bo);
            int i = userService.updateUser(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 根据ID删除用户
     * @param userId : 用户ID
     * @return
     */
    @DeleteMapping("/{userId}")
    @ApiOperation("根据ID删除用户")
    @ApiImplicitParam(name = "userId",value = "用户ID",required = true,paramType ="path")
    public Result deleteUser(@PathVariable("userId") Long userId){
        try {
            int i = userService.deleteUser(userId);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

}
